package at.fhhgb.mc.android.mfb_hgb.dal;

import java.util.Arrays;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import at.fhhgb.mc.android.mfb_hgb.control.JSONParser;
import at.fhhgb.mc.android.mfb_hgb.model.TaskCaller;

public class UpdateUser extends AsyncTask<NameValuePair, Void, Integer> {
	private JSONParser jParser = new JSONParser();
	private TaskCaller caller;
	
	private static final String URL_INSERT_USER = "http://jupuuu.funpic.de/mySQL/update_user.php";
	
	/**
	 * Default constructor
	 * @param caller The caller of AsyncTask
	 */
	public UpdateUser(TaskCaller caller)
	{
		this.caller = caller;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	// Getting Users for parameters
	@Override
	protected Integer doInBackground(NameValuePair... params)  {		
		// getting JSON string from URL
		JSONObject json = jParser.makeHttpRequest(URL_INSERT_USER, "POST", Arrays.asList(params));

		try {
			// Checking for SUCCESS TAG
			return json.getInt("success");
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return 0;
	}

	/**
	 * After completing background task
	**/
	@Override
	protected void onPostExecute(Integer result) {
		caller.onBackgroundTaskCompleted(result);
	}
}
